package com.study.mybatis.binding;

import com.study.mybatis.session.Configuration;
import com.study.mybatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yuantongqin
 * 2021/7/31
 *
 **/
public class MapperRegistry {

    private final Configuration config;
    /**
     * 为每一个userMapper.java 生成一个代理对象
     */
    private Map<Class<?>,MapperProxyFactory<?>> knownMappers = new ConcurrentHashMap<>();


    public MapperRegistry(Configuration config) {
        this.config = config;
    }

    public <T> void addMapperProxy(Class<T> tClass){
        knownMappers.put(tClass,new MapperProxyFactory<>(tClass));
    }


    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
        return mapperProxyFactory.getMapperProxy(sqlSession);
    }





}
